package org.stream.transfer;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Config {

    static Properties properties = new Properties();
    static {

        try(InputStream in =Transfer.class.getClassLoader().getResourceAsStream("config.properties")) {
            properties.load(in);
            if(in!=null) in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取字符串类型的值
     * @param key
     * @return
     */
    public static String get(String key) {
        return properties.getProperty(key);
    }

    public static String get(String key,String def) {
        String val=properties.getProperty(key);
        return val==null?def:val;
    }
    /**
     * 获取Integer类型的值
     * @param key
     * @return
     */
    public static Integer getInteger(String key) {

        String value = get(key);
        return null == value ? null : Integer.valueOf(value);
    }

    public static Integer getInteger(String key,Integer def) {

        String value = get(key);
        return null == value ? def : Integer.valueOf(value);
    }

    /**
     * 获取Boolean类型的值
     * @param key
     * @return
     */
    public static Boolean getBoolean(String key) {
        String value = get(key);
        return null == value ? null : Boolean.valueOf(value);
    }

    /**
     * 设置一个键值对
     * @param key
     * @param value
     */
    public static void set(String key,String value){
        properties.setProperty(key,value);
    }

    /**
     * 添加一个键值对
     * @param key
     * @param value
     */
    public static void add(String key,Object value){
        properties.put(key,value);
    }


}
